﻿using Photoshop;
using SharpShell.Attributes;
using SharpShell.ServerRegistration;
using SharpShell.SharpIconHandler;
using SharpShell.SharpThumbnailHandler;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Tisn
{
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".psd", ".pdd", ".psb")]
    [Guid("F4395619-2CF8-4D68-BABC-D3412E13C7C9")]
    public class PhotoshopFileThumbnail : SharpThumbnailHandler
    {
        protected override Bitmap GetThumbnailImage(uint width)
        {
            using (PsdFile psdFile = new PsdFile())
            {
                psdFile.Load(SelectedItemStream);
                Bitmap tmp = ImageDecoder.DecodeImage(psdFile);
                if (Utility.IsWhilteBitmap(tmp))
                {
                    tmp.Dispose();
                    tmp = ImageDecoder.DecodeImage(psdFile, true);
                }
                return Utility.ResizeBitmap(tmp, (int)width);
            }
        }
        [PrevCustomRegisterFunction]
        internal static void P(Type type, RegistrationType registrationType)
        {
            Utility.PrevReg(type, registrationType, Utility.PSD_Names);
        }
    }

    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".psd", ".pdd", ".psb")]
    [Guid("6ECD62C8-F646-4B76-A652-B5983B3A0911")]
    public class PhotoshopFilePSDIcon : SharpIconHandler
    {
        protected override Icon GetIcon(bool smallIcon, uint iconSize)
        {
            var ext = Path.GetExtension(SelectedItemPath);
            switch (ext)
            {
                case ".psd":
                    return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_psd, (int)iconSize).GetHicon());
                case ".psb":
                    return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_psb, (int)iconSize).GetHicon());
                case ".pdd":
                    return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_pdd, (int)iconSize).GetHicon());
            }
            return Icon.FromHandle(Utility.ResizeBitmap(Resource.icon_psd, (int)iconSize).GetHicon());
        }
        [PrevCustomRegisterFunction]
        internal static void P(Type type, RegistrationType registrationType)
        {
            Utility.PrevReg(type, registrationType, Utility.PSD_Names);
        }
    }
}
